//
//  jwt.swift
//  hello
//
//  Created by yafei li on 2025/9/17.
//

import JWT
import Vapor

struct JWTConfig {
    static func configure(app: Application) async throws {
        // 读取密钥
        let privateKeyData = try Data(contentsOf: URL(fileURLWithPath: "Resources/private.pem"))
        let publicKeyData = try Data(contentsOf: URL(fileURLWithPath: "Resources/public.pem"))
        let privateKeyDataPem = String(decoding: privateKeyData, as: UTF8.self)
        let publicKeyDataPem = String(decoding: publicKeyData, as: UTF8.self)
        // 使用公共 pem 初始化 RSA 密钥。
        let privateKey =  try Insecure.RSA.PrivateKey(pem: privateKeyDataPem)
        let publicKey = try Insecure.RSA.PublicKey(pem: publicKeyDataPem)
        // 添加密钥到 JWT 配置
        try await app.jwt.keys.add(rsa: Insecure.RSA.PublicKey(pem: publicKey.pemRepresentation), digestAlgorithm: .sha256) 
        try await app.jwt.keys.add(rsa: Insecure.RSA.PrivateKey(pem: privateKey.pemRepresentation), digestAlgorithm: .sha256)
    }
    
}
